package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.consts.ConfigConstant;
import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.sys.core.annotion.ApiToken;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import weixin.WeChatApi;
import weixin.utils.HttpUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author txl
 * @version 1.0
 * @date 2021/4/14 9:25
 * @description
 */
@RestController
@RequestMapping("/api/DeviceManagement")
@Api(tags = "微信小程序设备管理")
public class DeviceManagement {

    /**
     * 创建设备
     */
    @RequestMapping(value = "/addQservice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("创建设备")
    @ApiToken
    public ResponseData addQservice(@RequestHeader("Authorization") String token, @RequestParam("list") List<String> list) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SESSIONID", ConstantsContext.getSessionId());
        jsonObject.put("METHOD", "ADDDEVICE");
        if (list.size() > 0) {
            JSONArray jsonArray = new JSONArray();
            for (String bean : list) {
                JSONObject object = new JSONObject();
                object.put("SN", bean);
                jsonArray.add(object);
            }
            jsonObject.put("DEVICELIST", jsonArray);
        } else {
            return ResponseData.error("没有要创建的设备");
        }
        String requestJson = HttpUtils.sendPostRequestJson("https://iotservice.rhonergy.com:8888/qservice", jsonObject);
        JSONObject object = JSONObject.parseObject(requestJson);
        return ResponseData.success(object);
    }

    /**
     * 修改设备
     */
    @RequestMapping(value = "/updateQservice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("修改设备")
    @ApiToken
    public ResponseData updateQservice(@RequestHeader("Authorization") String token, @RequestBody Device device) {
        if (ToolUtil.isEmpty(device.getSN())){
            return ResponseData.error("SN码不能为空");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SESSIONID", ConstantsContext.getSessionId());
        jsonObject.put("METHOD", "SETDEVICE");
        JSONArray jsonArray = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("SN", device.getSN());
        if (ToolUtil.isNotEmpty(device.getDeviceName())){
            object.put("DeviceName", device.getDeviceName());
        }
        if (ToolUtil.isNotEmpty(device.getSMSNotificationEnabe())){
            object.put("SMSNotificationEnabe", device.getSMSNotificationEnabe());
        }
        if (ToolUtil.isNotEmpty(device.getSMSReceiver1())){
            object.put("SMSReceiver1", device.getSMSReceiver1());
        }
        if (ToolUtil.isNotEmpty(device.getSMSReceiver2())){
            object.put("SMSReceiver2", device.getSMSReceiver2());
        }
        if (ToolUtil.isNotEmpty(device.getVoiceNotificationEnable())){
            object.put("VoiceNotificationEnable", device.getVoiceNotificationEnable());
        }
        if (ToolUtil.isNotEmpty(device.getVoiceReceiver())){
            object.put("VoiceReceiver", device.getVoiceReceiver());
        }
        jsonArray.add(object);
        jsonObject.put("DEVICELIST", jsonArray);
        String requestJson = HttpUtils.sendPostRequestJson("https://iotservice.rhonergy.com:8888/qservice", jsonObject);
        JSONObject objectRequestJson = JSONObject.parseObject(requestJson);
        return ResponseData.success(objectRequestJson);
    }

    /**
     * 删除设备
     */
    @RequestMapping(value = "/deteleQservice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("删除设备")
    @ApiToken
    public ResponseData deteleQservice(@RequestHeader("Authorization") String token, @RequestParam("SN") String SN) {
        if (ToolUtil.isEmpty(SN)){
            return ResponseData.error("SN码不能为空");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SESSIONID", ConstantsContext.getSessionId());
        jsonObject.put("METHOD", "DELDEVICE");
        jsonObject.put("SN", SN);
        String requestJson = HttpUtils.sendPostRequestJson("https://iotservice.rhonergy.com:8888/qservice", jsonObject);
        JSONObject object = JSONObject.parseObject(requestJson);
        return ResponseData.success(object);
    }

    /**
     * 查询设备列表
     */
    @RequestMapping(value = "/selectQservice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("查询设备列表")
    @ApiToken
    public ResponseData selectQservice(@RequestHeader("Authorization") String token, @RequestParam("SN") String SN) {
        if (ToolUtil.isEmpty(SN)){
            return ResponseData.error("SN码不能为空");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SESSIONID", ConstantsContext.getSessionId());
        jsonObject.put("METHOD", "GETDEVICE");
        jsonObject.put("SN", SN);
        String requestJson = HttpUtils.sendPostRequestJson("https://iotservice.rhonergy.com:8888/qservice", jsonObject);
        JSONObject object = JSONObject.parseObject(requestJson);
        return ResponseData.success(object);
    }
}
